package com.oocl.euc.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.oocl.csc.frm.integration.configuration.loader.servlet.FWConfigLoaderServlet;
import com.oocl.euc.authentication.core.AuthUrlSetting;
import com.oocl.euc.authentication.tool.CookieHelper;
import com.oocl.euc.authentication.tool.SsoTokenHelper;
import com.oocl.euc.authentication.tool.WebHelper;
import com.oocl.euc.core.maintenance.service.RoleService;
import com.oocl.euc.core.maintenance.service.UsersService;
import com.oocl.euc.filter.GenerateTokenFilter;
import com.oocl.euc.filter.JwtAuthenticationFilter;
import com.oocl.euc.filter.PcrsCorsFilter;
import com.oocl.euc.filter.SSOAuthFilter;
import com.oocl.euc.framework.authentication.jwt.JwtSettings;
import com.oocl.euc.framework.authentication.jwt.TokenExtractor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.http.HttpServlet;

@Configuration
public class AuthenticationConfig {

    @Autowired
    private SsoTokenHelper tokenHelper;
    @Autowired
    private TokenExtractor jwtTokenExtractor;
    @Autowired
    private JwtSettings jwtSettings;
    @Autowired
    private AuthUrlSetting urlSetting;
    @Autowired
    private CookieHelper cookieHelper;
    @Autowired
    private UsersService usersService;
    @Autowired
    private RoleService roleService;
    @Autowired
    ObjectMapper objectMapper;
    @Autowired
    private WebHelper webHelper;

    @Bean
    public ServletRegistrationBean fwConfigLoaderServletRegistration() {
        HttpServlet fwConfigLoaderServlet = new FWConfigLoaderServlet();
        ServletRegistrationBean registration = new ServletRegistrationBean(fwConfigLoaderServlet, "/any");
        registration.addInitParameter("APP_NAME", "PCRS_app");
        registration.addInitParameter("APP_SERVER_INSTANCE_NAME", "PCRS_app_server");
        registration.addInitParameter("APP_CONFIG_FILE_PATH", "config.xml");
        registration.setName("FWConfigLoaderServlet");
        return registration;
    }

    @Bean
    public FilterRegistrationBean generateTokenFilterRegistration() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new GenerateTokenFilter(tokenHelper, cookieHelper, usersService, roleService, webHelper));
        registration.addUrlPatterns(urlSetting.getSsoAuthUrlPattern());
        registration.setName("GenerateTokenFilter");
        registration.setOrder(3);
        return registration;
    }

    @Bean
    public FilterRegistrationBean jwtAuthenticationFilter() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new JwtAuthenticationFilter(jwtTokenExtractor, tokenHelper, jwtSettings, cookieHelper, objectMapper));
        registration.addUrlPatterns(urlSetting.getJwtAuthUrlPattern());
        registration.setName("JwtAuthenticationFilter");
        registration.setOrder(4);
        return registration;
    }

    @Bean
    public FilterRegistrationBean pcrsCorsFilter() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new PcrsCorsFilter());
        registration.addUrlPatterns("/*");
        registration.setName("PcrsCorsFilter");
        registration.setOrder(1);
        return registration;
    }

    @Bean
    public FilterRegistrationBean ssoAuthFilter() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new SSOAuthFilter(webHelper));
        registration.addUrlPatterns(urlSetting.getSsoAuthUrlPattern());
        registration.addInitParameter("except_list", "/except");
        registration.addInitParameter("service_account_name", "pcrsServiceAccount");
        registration.setName("SSOAuthFilter");
        registration.setOrder(2);
        return registration;
    }
}
